﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;

namespace METU_CMT.Business
{
    public class Course
    {
        protected List<Announcement> announcements;
        protected List<Event> schedule;
        protected List<Instructor> instructors;
        protected List<Note> lectureNotes;
        protected List<ReviewTeam> reviewTeams;
        protected List<TeachingAssistant> tas;
        protected List<Team> teams;
        protected List<Student> students;
        protected string courseCode;
        protected string courseName;
        protected string semester;
        protected int courseId;


        public Course(int courseId)
        {
            CourseTableAdapter _courseTableAdapter = new CourseTableAdapter();
            courseCode = _courseTableAdapter.GetCourseCode(courseId);
            courseName = _courseTableAdapter.GetCourseName(courseId);
            semester = _courseTableAdapter.GetCourseSemester(courseId);
            announcements = _courseTableAdapter.GetAnnouncements(courseId);
            schedule = _courseTableAdapter.GetSchedule(courseId);
            instructors = _courseTableAdapter.GetInstructors(courseId);
            lectureNotes = _courseTableAdapter.GetLectureNotes(courseId);
            reviewTeams = _courseTableAdapter.GetReviewTeams(courseId);
            tas = _courseTableAdapter.GetTas(courseId);
            teams = _courseTableAdapter.GetTeams(courseId);
            students = _courseTableAdapter.GetStudents(courseId);
            this.courseId = courseId;
        }

        protected Course()
        {
            //TODO: Not Implemented
        }

        public List<Announcement> Announcements
        {
            get { return announcements; }
            set { announcements = value; }
        }

        public List<Event> Schedule
        {
            get { return schedule; }
            set { schedule = value; }
        }

        public List<Instructor> Instructors
        {
            get { return instructors; }
            set { instructors = value; }
        }

        public List<Note> LectureNotes
        {
            get { return lectureNotes; }
            set { lectureNotes = value; }
        }

        public List<ReviewTeam> ReviewTeams
        {
            get { return reviewTeams; }
            set { reviewTeams = value; }
        }

        public List<TeachingAssistant> TAs
        {
            get { return tas; }
            set { tas = value; }
        }

        public List<Team> Teams
        {
            get { return teams; }
            set { teams = value; }
        }

        public string CourseCode
        {
            get { return courseCode; }
            set { courseCode = value; }
        }

        public string CourseName
        {
            get { return courseName; }
            set { courseName = value; }
        }

        public string Semester
        {
            get { return semester; }
            set { semester = value; }
        }

        public int CourseId
        {
            get { return courseId; }
            set { courseId = value; }
        }

        public List<Student> Students
        {
            get { return students; }
            set { students = value; }
        }

        public List<Event> GetSubmissionEvents()
        {
            return schedule.Where(anEvent => anEvent.SubmissionExist == true).ToList();
        }

        private List<ReviewTeam> FindReviewTeamsOfStudent(Student student)
        {
            return reviewTeams.Where(
                reviewTeam => reviewTeam.ReviewerTeam.Members.Any(_student => _student.UserId == student.UserId)).
                ToList();
        }

        public void GetStudentsReviewTeamAssigments(Student student, out List<LocalEventDocument> localEventDocuments)
        {
            List<ReviewTeam> _reviewTeams = FindReviewTeamsOfStudent(student);

            localEventDocuments = new List<LocalEventDocument>();
            foreach (ReviewTeam _reviewTeam in _reviewTeams)
            {
                LocalEventDocument _localEventDocument = new LocalEventDocument();
                Deliverable _d = _reviewTeam.ReviewedTeam.Deliverables.FindLast(
                    deliverable => deliverable.Event.EventId == _reviewTeam.Event.EventId);

                if (_d != null && _d.Event.DueDate.CompareTo(DateTime.Now) < 0)
                {
                    _localEventDocument.DeliverableId = _d.DeliverableId;
                    _localEventDocument.DocumentName = _d.File;
                    _localEventDocument.UploadTime = _d.UploadTime;
                }



                _localEventDocument.TeamId = _reviewTeam.ReviewedTeam.TeamId;
                _localEventDocument.TeamName = _reviewTeam.ReviewedTeam.TeamName;
                _localEventDocument.Description = _reviewTeam.Event.Description;
                _localEventDocument.EventId = _reviewTeam.Event.EventId;
                localEventDocuments.Add(_localEventDocument);
            }
        }

        public virtual void GoToCourseMainPage(HttpResponse response)
        {
            HttpContext.Current.Session["MenuItem2"] = "~/StudentCourseMainScreen.aspx";
            response.Redirect("~/StudentCourseMainScreen.aspx");
        }

        public Team GetTeamOfStudent(Student student)
        {
            return teams.Find(team => team.Members.Any(_student => _student.UserId == student.UserId));
        }

        public List<LocalEventDocument> GetScheduleWithWork()
        {
            List<LocalEventDocument> _localEventDocuments = new List<LocalEventDocument>();
            Student _student = (Student)EntityFactory.User;
            foreach (Event _event in schedule)
            {
                LocalEventDocument _localEventDocument = new LocalEventDocument();
                Team _team = GetTeamOfStudent(_student);
                if (_team == null)
                {
                    return _localEventDocuments;
                }

                Deliverable _deliverable = _team.Deliverables.FindLast(deliverable => deliverable.Event.EventId == _event.EventId);
                if (_deliverable != null)
                {
                    _localEventDocument.DocumentName = _deliverable.File;
                    _localEventDocument.DeliverableId = _deliverable.DeliverableId;
                    _localEventDocument.UploadTime = _deliverable.UploadTime;
                    _localEventDocument.SubmitterUserName = new UsersTableAdapter().GetUserName(_deliverable.SubmitterId);
                }
                else
                {
                    _localEventDocument.UploadTime = null;
                }

                _localEventDocument.Description = _event.Description;
                _localEventDocument.DueDate = _event.DueDate;
                _localEventDocument.EventId = _event.EventId;
                _localEventDocument.TeamId = _team.TeamId;
                _localEventDocument.TeamName = _team.TeamName;

                _localEventDocuments.Add(_localEventDocument);
            }
            return _localEventDocuments;
        }
    }
}